﻿using Newtonsoft.Json.Linq;
using NPortableEngine;
using Services4KouDai.Models;
using System;
using System.Diagnostics;
using System.Collections.Generic;

namespace Services4KouDai.Parsers
{
    public class GroupCombineParser : IParser
    {
        private Platform _platform;

        public GroupCombineParser(Platform platform)
        {
            _platform = platform;
        }

        public override void ParseJson(string json, BackData backData)
        {
            List<GroupCombineEntity> groupItems = (List<GroupCombineEntity>)backData.ResponseData;

            if (_platform == Platform.WIN8)
            {
                JObject jobj = JObject.Parse(json);

                JToken jarray = jobj.SelectToken("result");

                foreach (JToken jt in jarray.Children())
                {
                    GroupCombineEntity groupCombineEntity = new GroupCombineEntity();
                    groupCombineEntity.GroupId = jt.Value<string>("groupId");
                    groupCombineEntity.GroupName = jt.Value<string>("groupName");
                    groupCombineEntity.GroupType = jt.Value<int>("groupType");
                    groupCombineEntity.Description = jt.Value<string>("description");
                    groupCombineEntity.LastUpdateTime = jt.Value<string>("latestUpdateTime");

                    OperationShopList(groupCombineEntity, jt);

                    groupItems.Add(groupCombineEntity);
                }
            }
            else if (_platform == Platform.WP8)
            {
                JArray jarray = JArray.Parse(json);

                foreach (JToken jt in jarray.Children())
                {
                    GroupCombineEntity groupCombineEntity = new GroupCombineEntity();
                    groupCombineEntity.GroupId = jt.Value<string>("groupId");
                    groupCombineEntity.GroupName = jt.Value<string>("groupName");
                    groupCombineEntity.GroupType = jt.Value<int>("groupType");
                    groupCombineEntity.Description = jt.Value<string>("description");
                    groupCombineEntity.LastUpdateTime = jt.Value<string>("latestUpdateTime");

                    OperationShopList(groupCombineEntity, jt);
                    
                    groupItems.Add(groupCombineEntity);
                }
            }
            
        }

        private void OperationShopList(GroupCombineEntity groupCombineEntity,JToken jt)
        {
            JToken subJt = jt.SelectToken("shopList");
            foreach (JToken item in subJt.Children())
            {
                ShopEntity shop = new ShopEntity();
                shop.AppName = item.Value<string>("appName");
                shop.Description = item.Value<string>("description");
                shop.EntranceName = item.Value<string>("entranceName");
                shop.Remarks = item.Value<string>("remarks");
                shop.Score = item.Value<int>("score");
                shop.ShopGrade = item.Value<string>("shopGrade");
                groupCombineEntity.ShopList.Add(shop);
            }
        }
    }
}
